package com.hardson.toolkit.swing.print;

import java.awt.print.PageFormat;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;

import javax.swing.JComponent;

public class PrintFactory {

	private static PrintFactory instance;

	private JComponentVista vista;

	public static PrintFactory getInstance() {
		if (instance == null) {
			instance = new PrintFactory();
		}
		return instance;
	}

	private PrintFactory() {
	}

	public void printJComponent(final JComponent c, final PageFormat format) {
		this.vista = new JComponentVista(c, format);
		this.vista.scaleToFitX();
		PrinterJob pj = PrinterJob.getPrinterJob();
		pj.setPageable(this.vista);
		try {
			if (pj.printDialog()) {
				pj.print();
			}
		} catch (PrinterException e) {
			e.printStackTrace();
		}
	}

	public void printJComponent(final JComponent c) {
		this.printJComponent(c, new PageFormat());
	}
}
